<template>
    <el-card>
        <el-form :inline="true" class="form">
            <el-form-item label="职位搜索">
                <el-input placeholder="请你输入搜索职位关键字" v-model="keyword"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="default" @click="search">搜索</el-button>
                <el-button type="primary" size="default" @click="reset">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
    <el-card style="margin: 10px 0px;">
        <el-button type="primary" size="default" icon="Plus" @click="addRole">添加职位</el-button>
        <el-table border style="margin: 10px 0px;" :data="allRole">
            <el-table-column type="index" align="center" label="#"></el-table-column>
            <el-table-column label="ID" align="center" prop="id"></el-table-column>
            <el-table-column label="职位名称" align="center" prop="roleName" show-overflow-tooltip></el-table-column>
            <el-table-column label="创建世间" align="center" show-overflow-tooltip prop="createTime"></el-table-column>
            <el-table-column label="更新时间" align="center" show-overflow-tooltip prop="updateTime"></el-table-column>
            <el-table-column label="操作" width="280px" align="center">
                <!-- row:已有的职位对象 -->
                <template #="{ row }">
                    <el-button type="primary" size="small" icon="User" @click="setPermisstion(row)">分配权限</el-button>
                    <el-button type="primary" size="small" icon="Edit" @click="updateRole(row)">编辑</el-button>
                    <el-popconfirm :title="`你确定要删除${row.roleName}?`" width="260px" @confirm="removeRole(row.id)">
                        <template #reference>
                            <el-button type="primary" size="small" icon="Delete">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40]"
            :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total" @current-change="getHasRole"
            @size-change="sizeChange" />
    </el-card>
    <!-- 添加职位与更新已有职位的结构:对话框 -->
    <el-dialog v-model="dialogVisite" :title="RoleParams.id ? '更新职位' : '添加职位'">
        <el-form :model="RoleParams" :rules="rules" ref="form">
            <el-form-item label="职位名称" prop="roleName">
                <el-input placeholder="请你输入职位名称" v-model="RoleParams.roleName"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="primary" size="default" @click="dialogVisite = false">取消</el-button>
            <el-button type="primary" size="default" @click="save">确定</el-button>
        </template>
    </el-dialog>
    <!-- 抽屉组件:分配职位的菜单权限与按钮的权限 -->
    <el-drawer v-model="drawer">
        <template #header>
            <h4>分配菜单与按钮的权限</h4>
        </template>
        <template #default>
            <!-- 树形控件 -->
            <el-tree ref="tree" :data="menuArr" show-checkbox node-key="id" default-expand-all
                :default-checked-keys="selectArr" :props="defaultProps" />
        </template>
        <template #footer>
            <div style="flex: auto">
                <el-button @click="drawer = false">取消</el-button>
                <el-button type="primary" @click="handler">确定</el-button>
            </div>
        </template>
    </el-drawer>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive, nextTick } from 'vue';
//请求方法
import { reqRemoveRole, reqAllRoleList, reqAddOrUpdateRole, reqAllMenuList, reqSetPermisstion } from '@/api/acl/role';
import type { RoleResponseData, Records, RoleData, MenuResponseData, MenuList } from '@/api/acl/role/type'
//引入骨架的仓库
import useLayOutSettingStore from '@/store/modules/setting';
import { ElMessage } from 'element-plus';
let settingStore = useLayOutSettingStore();
//当前页码
let pageNo = ref<number>(1);
//一页展示几条数据
let pageSize = ref<number>(10);
//搜索职位关键字
let keyword = ref<string>('');
//存储全部已有的职位
let allRole = ref<Records>([]);
//职位总个数
let total = ref<number>(0);
//控制对话框的显示与隐藏
let dialogVisite = ref<boolean>(false);
//获取form组件实例
let form = ref<any>();
//控制抽屉显示与隐藏
let drawer = ref<boolean>(false);
//收集新增岗位数据
let RoleParams = reactive<RoleData>({
    roleName: ''
})
//准备一个数组:数组用于存储勾选的节点的ID(四级的)
let selectArr = ref<number[]>([]);
//定义数组存储用户权限的数据
let menuArr = ref<MenuList>([]);
//获取tree组件实例
let tree = ref<any>();
//组件挂载完毕
onMounted(() => {
    //获取职位请求
    getHasRole();
});
//获取全部用户信息的方法|分页器当前页码发生变化的回调
const getHasRole = async (pager = 1) => {
    //修改当前页码
    pageNo.value = pager;
    let result: RoleResponseData = await reqAllRoleList(pageNo.value, pageSize.value, keyword.value);
    if (result.code == 200) {
        total.value = result.data.total;
        allRole.value = result.data.records;
    }
    // records 数据示例
    /* [
        {
            "id": 16068,
            "createTime": "2024-09-01 16:42:58",
            "updateTime": "2024-09-01 16:42:58",
            "roleName": "441725183111",
            "remark": null
        },
        {
            "id": 16066,
            "createTime": "2024-09-01 13:40:52",
            "updateTime": "2024-09-01 13:40:52",
            "roleName": "工程师",
            "remark": null
        },
        {
            "id": 16065,
            "createTime": "2024-09-01 13:36:18",
            "updateTime": "2024-09-01 13:36:18",
            "roleName": "学者",
            "remark": null
        },
        {
            "id": 16060,
            "createTime": "2024-08-31 01:53:05",
            "updateTime": "2024-08-31 21:21:29",
            "roleName": "HR13",
            "remark": null
        },
        {
            "id": 16058,
            "createTime": "2024-08-31 01:53:00",
            "updateTime": "2024-08-31 01:53:00",
            "roleName": "学生员",
            "remark": null
        },
        {
            "id": 16057,
            "createTime": "2024-08-31 01:52:17",
            "updateTime": "2024-08-31 01:52:17",
            "roleName": "自由职业",
            "remark": null
        },
        {
            "id": 16056,
            "createTime": "2024-08-31 01:52:11",
            "updateTime": "2024-08-31 01:52:11",
            "roleName": "教师员",
            "remark": null
        },
        {
            "id": 16055,
            "createTime": "2024-08-31 01:51:17",
            "updateTime": "2024-08-31 01:51:17",
            "roleName": "飞行员",
            "remark": null
        },
        {
            "id": 1,
            "createTime": "2021-05-31 18:09:18",
            "updateTime": "2023-04-28 11:03:38",
            "roleName": "超级管理员",
            "remark": null
        }
    ] */
}
//下拉菜单的回调
const sizeChange = () => {
    getHasRole();
}
//搜索按钮的回调
const search = () => {
    //再次发请求根据关键字
    getHasRole();
    // keyword.value = '';
}
//重置按钮的回调
const reset = () => {
    settingStore.refresh = !settingStore.refresh;
    getHasRole()
}
//添加职位按钮的回调
const addRole = () => {
    //对话框显示出来
    dialogVisite.value = true;
    //清空数据
    Object.assign(RoleParams, {
        roleName: '',
        id: 0
    });
    //清空上一次表单校验错误结果
    nextTick(() => {
        form.value.clearValidate('roleName');
    })

}
//更新已有的职位按钮的回调
const updateRole = (row: RoleData) => {
    //显示出对话框
    dialogVisite.value = true;
    //存储已有的职位----带有ID的
    Object.assign(RoleParams, row);
    //清空上一次表单校验错误结果
    nextTick(() => {
        form.value.clearValidate('roleName');
    })
}
//自定义校验规则的回调
const validatorRoleName = (rule: any, value: any, callBack: any) => {
    if (value.trim().length >= 2) {
        callBack();
    } else {
        callBack(new Error('职位名称至少两位'))
    }
}
//职位校验规则
const rules = {
    roleName: [
        { required: true, trigger: 'blur', validator: validatorRoleName }
    ]
}

//确定按钮的回调
const save = async () => {
    //表单校验结果,结果通过在发请求、结果没有通过不应该在发生请求
    await form.value.validate();
    //添加职位|更新职位的请求
    let result: any = await reqAddOrUpdateRole(RoleParams);
    if (result.code == 200) {
        //提示文字
        ElMessage({ type: 'success', message: RoleParams.id ? '更新成功' : '添加成功' });
        //对话框显示
        dialogVisite.value = false;
        //再次获取全部的已有的职位
        getHasRole(RoleParams.id ? pageNo.value : 1);
    }
}

//分配权限按钮的回调
//已有的职位的数据
const setPermisstion = async (row: RoleData) => {
    //抽屉显示出来
    drawer.value = true;
    //收集当前要分类权限的职位的数据
    Object.assign(RoleParams, row);
    //根据职位获取权限的数据
    let result: MenuResponseData = await reqAllMenuList((RoleParams.id as number));
    if (result.code == 200) {
        menuArr.value = result.data;
        selectArr.value = filterSelectArr(menuArr.value, []);
    }
    // result.data
    /* [
        {
            "id": 1,
            "createTime": "2019-11-15 17:13:06",
            "updateTime": "2020-09-25 13:47:54",
            "pid": 0,
            "name": "全部数据",
            "code": null,
            "toCode": null,
            "type": 1,
            "status": null,
            "level": 1,
            "children": [
                {
                    "id": 7,
                    "createTime": "2020-11-30 16:40:08",
                    "updateTime": "2021-12-04 19:39:41",
                    "pid": 1,
                    "name": "权限管理",
                    "code": "Acl",
                    "toCode": "",
                    "type": 1,
                    "status": null,
                    "level": 2,
                    "children": [
                        {
                            "id": 8,
                            "createTime": "2020-11-30 16:40:38",
                            "updateTime": "2021-12-04 19:40:01",
                            "pid": 7,
                            "name": "用户管理",
                            "code": "User",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 11,
                                    "createTime": "2020-11-30 16:43:16",
                                    "updateTime": "2021-12-04 19:42:37",
                                    "pid": 8,
                                    "name": "添加用户",
                                    "code": "btn.User.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 12,
                                    "createTime": "2020-11-30 16:45:00",
                                    "updateTime": "2021-12-04 19:42:39",
                                    "pid": 8,
                                    "name": "删除用户",
                                    "code": "btn.User.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 13,
                                    "createTime": "2020-11-30 16:45:32",
                                    "updateTime": "2021-12-04 19:42:41",
                                    "pid": 8,
                                    "name": "修改用户",
                                    "code": "btn.User.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 14,
                                    "createTime": "2020-11-30 16:46:08",
                                    "updateTime": "2021-12-04 19:42:43",
                                    "pid": 8,
                                    "name": "分配角色",
                                    "code": "btn.User.assgin",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                }
                            ],
                            "select": true
                        },
                        {
                            "id": 9,
                            "createTime": "2020-11-30 16:40:55",
                            "updateTime": "2021-12-04 19:40:02",
                            "pid": 7,
                            "name": "角色管理",
                            "code": "Role",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 15,
                                    "createTime": "2020-11-30 16:50:13",
                                    "updateTime": "2021-12-04 19:42:45",
                                    "pid": 9,
                                    "name": "分配权限",
                                    "code": "btn.Role.assgin",
                                    "toCode": "RoleAuth",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 16,
                                    "createTime": "2020-11-30 16:50:56",
                                    "updateTime": "2021-12-04 19:42:48",
                                    "pid": 9,
                                    "name": "添加角色",
                                    "code": "btn.Role.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 17,
                                    "createTime": "2020-11-30 16:51:22",
                                    "updateTime": "2021-12-04 19:42:50",
                                    "pid": 9,
                                    "name": "修改角色",
                                    "code": "btn.Role.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 18,
                                    "createTime": "2020-11-30 16:52:34",
                                    "updateTime": "2021-12-04 19:42:53",
                                    "pid": 9,
                                    "name": "删除角色",
                                    "code": "btn.Role.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                },
                                {
                                    "id": 14137,
                                    "createTime": "2024-08-14 16:21:50",
                                    "updateTime": "2024-08-14 16:21:50",
                                    "pid": 9,
                                    "name": "查看角色",
                                    "code": "check",
                                    "toCode": null,
                                    "type": 0,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": true
                                }
                            ],
                            "select": true
                        },
                        {
                            "id": 10,
                            "createTime": "2020-11-30 16:41:21",
                            "updateTime": "2021-12-04 19:40:06",
                            "pid": 7,
                            "name": "菜单管理",
                            "code": "Permission",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 19,
                                    "createTime": "2020-11-30 16:56:23",
                                    "updateTime": "2021-12-04 19:42:55",
                                    "pid": 10,
                                    "name": "添加",
                                    "code": "btn.Permission.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 20,
                                    "createTime": "2020-11-30 16:59:02",
                                    "updateTime": "2021-12-04 19:42:58",
                                    "pid": 10,
                                    "name": "修改",
                                    "code": "btn.Permission.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 21,
                                    "createTime": "2020-11-30 16:59:27",
                                    "updateTime": "2021-12-04 19:43:00",
                                    "pid": 10,
                                    "name": "删除",
                                    "code": "btn.Permission.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 14494,
                            "createTime": "2024-08-30 22:48:36",
                            "updateTime": "2024-08-30 22:48:36",
                            "pid": 7,
                            "name": "",
                            "code": "",
                            "toCode": null,
                            "type": 0,
                            "status": null,
                            "level": 3,
                            "children": [],
                            "select": false
                        }
                    ],
                    "select": true
                },
                {
                    "id": 22,
                    "createTime": "2020-12-01 08:15:00",
                    "updateTime": "2021-12-04 19:43:28",
                    "pid": 1,
                    "name": "商品管理",
                    "code": "Product",
                    "toCode": "",
                    "type": 1,
                    "status": null,
                    "level": 2,
                    "children": [
                        {
                            "id": 23,
                            "createTime": "2020-12-01 08:15:16",
                            "updateTime": "2021-12-04 19:43:38",
                            "pid": 22,
                            "name": "分类管理",
                            "code": "Category",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 44,
                                    "createTime": "2020-12-01 08:58:39",
                                    "updateTime": "2021-12-04 19:47:48",
                                    "pid": 23,
                                    "name": "添加子分类",
                                    "code": "btn.Category.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 45,
                                    "createTime": "2020-12-01 08:59:00",
                                    "updateTime": "2021-12-04 19:47:49",
                                    "pid": 23,
                                    "name": "修改分类",
                                    "code": "btn.Category.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 46,
                                    "createTime": "2020-12-01 08:59:25",
                                    "updateTime": "2021-12-04 19:47:52",
                                    "pid": 23,
                                    "name": "删除分类",
                                    "code": "btn.Category.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 24,
                            "createTime": "2020-12-01 08:15:34",
                            "updateTime": "2021-12-04 19:44:28",
                            "pid": 22,
                            "name": "平台属性管理",
                            "code": "Attr",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 50,
                                    "createTime": "2020-12-01 09:16:18",
                                    "updateTime": "2021-12-04 19:48:06",
                                    "pid": 24,
                                    "name": "添加属性",
                                    "code": "btn.Attr.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 51,
                                    "createTime": "2020-12-01 09:18:52",
                                    "updateTime": "2021-12-04 19:48:08",
                                    "pid": 24,
                                    "name": "更新属性",
                                    "code": "btn.Attr.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 52,
                                    "createTime": "2020-12-01 09:19:09",
                                    "updateTime": "2021-12-04 19:48:10",
                                    "pid": 24,
                                    "name": "删除属性",
                                    "code": "btn.Attr.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 25,
                            "createTime": "2020-12-01 08:15:48",
                            "updateTime": "2021-12-04 19:44:40",
                            "pid": 22,
                            "name": "品牌管理",
                            "code": "Trademark",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 47,
                                    "createTime": "2020-12-01 09:05:22",
                                    "updateTime": "2021-12-04 19:47:54",
                                    "pid": 25,
                                    "name": "添加品牌",
                                    "code": "btn.Trademark.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 48,
                                    "createTime": "2020-12-01 09:06:07",
                                    "updateTime": "2021-12-04 19:47:56",
                                    "pid": 25,
                                    "name": "修改品牌",
                                    "code": "btn.Trademark.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 49,
                                    "createTime": "2020-12-01 09:06:28",
                                    "updateTime": "2021-12-04 19:48:03",
                                    "pid": 25,
                                    "name": "删除品牌",
                                    "code": "btn.Trademark.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 13146,
                                    "createTime": "2024-06-26 15:38:20",
                                    "updateTime": "2024-06-26 15:38:20",
                                    "pid": 25,
                                    "name": "查询品牌",
                                    "code": "btn.Trademark.search",
                                    "toCode": null,
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 26,
                            "createTime": "2020-12-01 08:16:14",
                            "updateTime": "2021-12-04 19:44:52",
                            "pid": 22,
                            "name": "SPU管理",
                            "code": "Spu",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 53,
                                    "createTime": "2020-12-01 09:22:40",
                                    "updateTime": "2021-12-04 19:48:13",
                                    "pid": 26,
                                    "name": "添加SPU",
                                    "code": "btn.Spu.add",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 54,
                                    "createTime": "2020-12-01 09:24:30",
                                    "updateTime": "2021-12-04 19:48:15",
                                    "pid": 26,
                                    "name": "添加SKU",
                                    "code": "btn.Spu.addsku",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 55,
                                    "createTime": "2020-12-01 09:24:54",
                                    "updateTime": "2021-12-04 19:48:17",
                                    "pid": 26,
                                    "name": "更新Spu",
                                    "code": "btn.Spu.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 56,
                                    "createTime": "2020-12-01 09:25:27",
                                    "updateTime": "2021-12-04 19:48:19",
                                    "pid": 26,
                                    "name": "查看SKU列表",
                                    "code": "btn.Spu.skus",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 57,
                                    "createTime": "2020-12-01 09:25:52",
                                    "updateTime": "2021-12-04 19:48:21",
                                    "pid": 26,
                                    "name": "删除Spu",
                                    "code": "btn.Spu.delete",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 27,
                            "createTime": "2020-12-01 08:16:30",
                            "updateTime": "2021-12-04 19:45:05",
                            "pid": 22,
                            "name": "Sku管理",
                            "code": "Sku",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 58,
                                    "createTime": "2020-12-01 09:32:09",
                                    "updateTime": "2021-12-04 19:48:24",
                                    "pid": 27,
                                    "name": "Sku上下架",
                                    "code": "btn.Sku.updown",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 59,
                                    "createTime": "2020-12-01 09:33:42",
                                    "updateTime": "2021-12-04 19:48:27",
                                    "pid": 27,
                                    "name": "更新SKU",
                                    "code": "btn.Sku.update",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 60,
                                    "createTime": "2020-12-01 09:34:29",
                                    "updateTime": "2021-12-04 19:48:30",
                                    "pid": 27,
                                    "name": "Sku详情",
                                    "code": "btn.Sku.detail",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 61,
                                    "createTime": "2020-12-01 09:34:48",
                                    "updateTime": "2021-12-04 19:48:45",
                                    "pid": 27,
                                    "name": "删除Sku",
                                    "code": "btn.Sku.remove",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        }
                    ],
                    "select": false
                },
                {
                    "id": 28,
                    "createTime": "2020-12-01 08:18:07",
                    "updateTime": "2021-12-04 19:45:25",
                    "pid": 1,
                    "name": "订单管理",
                    "code": "Order",
                    "toCode": "",
                    "type": 1,
                    "status": null,
                    "level": 2,
                    "children": [
                        {
                            "id": 29,
                            "createTime": "2020-12-01 08:19:31",
                            "updateTime": "2021-12-04 19:45:43",
                            "pid": 28,
                            "name": "订单列表",
                            "code": "OrderList",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 41,
                                    "createTime": "2020-12-01 08:47:59",
                                    "updateTime": "2021-12-04 19:47:37",
                                    "pid": 29,
                                    "name": "查看订单详情",
                                    "code": "btn.OrderList.detail",
                                    "toCode": "OrderShow",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 42,
                                    "createTime": "2020-12-01 08:52:24",
                                    "updateTime": "2021-12-04 19:47:39",
                                    "pid": 29,
                                    "name": "退款",
                                    "code": "btn.OrderList.Refund",
                                    "toCode": "Refund",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 62,
                            "createTime": "2020-12-02 11:34:23",
                            "updateTime": "2021-12-04 19:48:48",
                            "pid": 28,
                            "name": "退款管理",
                            "code": "Refund",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [],
                            "select": false
                        },
                        {
                            "id": 13986,
                            "createTime": "2024-08-08 15:55:21",
                            "updateTime": "2024-08-08 15:55:21",
                            "pid": 28,
                            "name": "设计师",
                            "code": "designer",
                            "toCode": null,
                            "type": 0,
                            "status": null,
                            "level": 3,
                            "children": [],
                            "select": false
                        }
                    ],
                    "select": false
                },
                {
                    "id": 30,
                    "createTime": "2020-12-01 08:21:25",
                    "updateTime": "2021-12-04 19:46:05",
                    "pid": 1,
                    "name": "客户管理",
                    "code": "ClientUser",
                    "toCode": "",
                    "type": 1,
                    "status": null,
                    "level": 2,
                    "children": [
                        {
                            "id": 31,
                            "createTime": "2020-12-01 08:22:08",
                            "updateTime": "2021-12-04 19:46:20",
                            "pid": 30,
                            "name": "客户列表",
                            "code": "UserList",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 43,
                                    "createTime": "2020-12-01 08:53:49",
                                    "updateTime": "2021-12-04 19:47:45",
                                    "pid": 31,
                                    "name": "锁定客户",
                                    "code": "btn.UserList.lock",
                                    "toCode": "",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        }
                    ],
                    "select": false
                },
                {
                    "id": 32,
                    "createTime": "2020-12-01 08:23:06",
                    "updateTime": "2021-12-04 19:46:33",
                    "pid": 1,
                    "name": "优惠管理",
                    "code": "Discount",
                    "toCode": "",
                    "type": 1,
                    "status": null,
                    "level": 2,
                    "children": [
                        {
                            "id": 33,
                            "createTime": "2020-12-01 08:23:20",
                            "updateTime": "2021-12-04 19:46:50",
                            "pid": 32,
                            "name": "优惠活动管理",
                            "code": "Activity",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 35,
                                    "createTime": "2020-12-01 08:38:57",
                                    "updateTime": "2021-12-04 19:47:22",
                                    "pid": 33,
                                    "name": "添加活动",
                                    "code": "btn.Activity.add",
                                    "toCode": "ActivityAdd",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 36,
                                    "createTime": "2020-12-01 08:39:50",
                                    "updateTime": "2021-12-04 19:47:24",
                                    "pid": 33,
                                    "name": "修改活动",
                                    "code": "btn.Activity.update",
                                    "toCode": "ActivityEdit",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 37,
                                    "createTime": "2020-12-01 08:40:20",
                                    "updateTime": "2021-12-04 19:47:26",
                                    "pid": 33,
                                    "name": "活动规则",
                                    "code": "btn.Activity.rule",
                                    "toCode": "ActivityRule",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        },
                        {
                            "id": 34,
                            "createTime": "2020-12-01 08:23:46",
                            "updateTime": "2021-12-04 19:47:03",
                            "pid": 32,
                            "name": "优惠券管理",
                            "code": "Coupon",
                            "toCode": "",
                            "type": 1,
                            "status": null,
                            "level": 3,
                            "children": [
                                {
                                    "id": 38,
                                    "createTime": "2020-12-01 08:40:58",
                                    "updateTime": "2021-12-04 19:47:28",
                                    "pid": 34,
                                    "name": "添加优惠券",
                                    "code": "btn.Coupon.add",
                                    "toCode": "CouponAdd",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 39,
                                    "createTime": "2020-12-01 08:41:34",
                                    "updateTime": "2021-12-04 19:47:30",
                                    "pid": 34,
                                    "name": "修改优惠券",
                                    "code": "btn.Coupon.update",
                                    "toCode": "CouponEdit",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                },
                                {
                                    "id": 40,
                                    "createTime": "2020-12-01 08:42:24",
                                    "updateTime": "2021-12-04 19:47:33",
                                    "pid": 34,
                                    "name": "活动规则",
                                    "code": "btn.Coupon.rule",
                                    "toCode": "CouponRule",
                                    "type": 2,
                                    "status": null,
                                    "level": 4,
                                    "children": [],
                                    "select": false
                                }
                            ],
                            "select": false
                        }
                    ],
                    "select": false
                },
                {
                    "id": 100,
                    "createTime": "2021-12-04 19:36:19",
                    "updateTime": "2021-12-04 19:39:57",
                    "pid": 1,
                    "name": "全部",
                    "code": "btn.all",
                    "toCode": null,
                    "type": 2,
                    "status": null,
                    "level": 2,
                    "children": [],
                    "select": false
                }
            ],
            "select": true
        }
    ] */
}
//树形控件的测试数据
const defaultProps = {
    children: 'children',
    label: 'name',
}

const filterSelectArr = (allData: any, initArr: any) => {
    allData.forEach((item: any) => {
        // if (item.select && item.level == 4) {
        //     initArr.push(item.id);
        // }
        if (item.children && item.children.length > 0) {
            filterSelectArr(item.children, initArr);
        } else {
            if (item.select) {
                initArr.push(item.id);
            }
        }
    })
    console.log(1179, initArr);

    return initArr;
}

//抽屉确定按钮的回调
const handler = async () => {
    //职位的ID
    const roleId = (RoleParams.id as number);
    //选中节点的ID
    let arr = tree.value.getCheckedKeys();
    //半选的ID
    let arr1 = tree.value.getHalfCheckedKeys();
    let permissionId = arr.concat(arr1);
    //下发权限
    let result: any = await reqSetPermisstion(roleId, permissionId);
    if (result.code == 200) {
        //抽屉关闭
        drawer.value = false;
        //提示信息
        ElMessage({ type: 'success', message: '分配权限成功' });
        //页面刷新
        window.location.reload();
    }
}

//删除已有的职位
const removeRole = async (id: number) => {
    let result: any = await reqRemoveRole(id);
    if (result.code == 200) {
        //提示信息
        ElMessage({ type: 'success', message: '删除成功' });
        getHasRole(allRole.value.length > 1 ? pageNo.value : pageNo.value - 1);
    }
}
</script>

<style scoped>
.form {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
}
</style>